﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;

using Services;
using Domain;

namespace WebRole1
{
	public partial class ActiveTopics : System.Web.UI.Page
	{
		private void Page_Init(object sender, System.EventArgs e)
		{
			this.EnableViewState = false;
		}




		public System.Drawing.Color Color0
		{
			get
			{
				return System.Drawing.Color.FromArgb(180, 180, 180);
			}
		}

		public System.Drawing.Color Color1
		{
			get
			{
				return System.Drawing.Color.White;
			}
		}

		public System.Drawing.Color Color2
		{
			get
			{
				return System.Drawing.Color.FromArgb(238, 238, 238);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			Title = "Forum - Active topics";

			HtmlMeta metaDesc = new HtmlMeta();
			metaDesc.Name = "description";
			metaDesc.Content = "forum jongeren";
			Page.Header.Controls.Add(metaDesc);

			HtmlMeta metaKey = new HtmlMeta();
			metaKey.Name = "keywords";
			metaKey.Content = "forum jongeren";
			Page.Header.Controls.Add(metaKey);




			decimal start = System.DateTime.Now.Ticks;

			string username = "";

			if (HttpContext.Current.Request.QueryString.AllKeys.Contains("user"))
			{
				username = HttpContext.Current.Request.QueryString["user"].ToString();
			}

			//Int64 forumID = Int64.Parse(HttpContext.Current.Request.QueryString["ID"].ToString());

			int backcolorType = 0;

			TableRow tr = NewTableRow(backcolorType);
			tr.Width = Unit.Pixel(700);


			//Topicname
			TableCell tc = NewTableCell();
			tc.Width = Unit.Pixel(400);
			Label lblTopicName = new Label();
			lblTopicName.Text = "Topic";
			tc.Controls.Add(lblTopicName);
			tr.Cells.Add(tc);

			//Topicstarter
			tc = NewTableCell();
			tc.Width = Unit.Pixel(150);
			Label lblTopicStarter = new Label();
			lblTopicStarter.Text = "Topicstarter";
			tc.Controls.Add(lblTopicStarter);
			tr.Cells.Add(tc);



			//Last poster
			tc = NewTableCell();
			tc.Width = Unit.Pixel(200);
			Label lblLastPoster = new Label();
			lblLastPoster.Text = "Last post";
			tc.Controls.Add(lblLastPoster);
			tr.Cells.Add(tc);


			//Posts
			tc = NewTableCell();
			tc.Width = Unit.Pixel(45);
			Label lblPostcount = new Label();
			lblPostcount.Text = "Posts";
			tc.Controls.Add(lblPostcount);
			tr.Cells.Add(tc);

			//Views
			tc = NewTableCell();
			tc.Width = Unit.Pixel(45);
			Label lblViewcount = new Label();
			lblViewcount.Text = "Views";
			tc.Controls.Add(lblViewcount);
			tr.Cells.Add(tc);

			tblTopics.Rows.Add(tr);




			TopicService topicServ = new TopicService();
			IList<Topic> topics = null;

			try
			{
				topics = topicServ.getActiveTopicsNew(username);
			}
			catch
			{
				Page_Load(sender, e);
			}

			backcolorType = 1;
			foreach (Topic t in topics)
			{
				tr = NewTableRow(backcolorType);
				tr.Width = Unit.Pixel(700);

				//Forumname
				tc = NewTableCell();
				HyperLink hlTopicName = new HyperLink();
				hlTopicName.NavigateUrl = "~/topic/" + t.ID.ToString() + "/1/" + t.UrlTitle;

				if (t.Title.Length >= 65)
				{
					hlTopicName.Text = t.Title.Substring(0, 65) + "...";
				}
				else
				{
					hlTopicName.Text = t.Title;
				}

				tc.Controls.Add(hlTopicName);



				int i = 2;
				Int64 pagecount = ((t.Posts - 1) / 25) + 1;
				while (i <= pagecount)
				{
					Label lbl = new Label();
					lbl.Text = " ";
					tc.Controls.Add(lbl);

					HyperLink hlPage = new HyperLink();
					hlPage.NavigateUrl = "~/topic/" + t.ID.ToString() + "/" + i.ToString() + "/" + t.UrlTitle;
					hlPage.Text = i.ToString();
					tc.Controls.Add(hlPage);
					i++;
				}












				tr.Cells.Add(tc);

				//Topicstarter
				tc = NewTableCell();

				string TopicStarterName = "";
				int length = 0;
				if (t.TopicStarterName.Length > 15)
				{
					length = 15;
				}
				else
				{
					length = t.TopicStarterName.Length;
				}

				if (t.TopicStarterName.Length > 0)
				{
					TopicStarterName = t.TopicStarterName.Substring(0, length);
				}

				if (t.TopicStarterID != "")
				{
					HyperLink hlTopicStarter = new HyperLink();
					hlTopicStarter.NavigateUrl = "./Profile.aspx?user=" + t.TopicStarterName;

					hlTopicStarter.Text = TopicStarterName;
					tc.Controls.Add(hlTopicStarter);

					tr.Cells.Add(tc);
				}
				else
				{
					Label hlTopicStarter = new Label();

					hlTopicStarter.Text = TopicStarterName;
					tc.Controls.Add(hlTopicStarter);

					tr.Cells.Add(tc);
				}



				//Last poster
				tc = NewTableCell();

				Label lblLastPostOn = new Label();
				lblLastPostOn.Text = t.LastPostOn + " - ";
				tc.Controls.Add(lblLastPostOn);

				string LastPosterName = "";
				length = 0;
				if (t.LastPosterName.Length > 15)
				{
					length = 15;
				}
				else
				{
					length = t.LastPosterName.ToString().Length;
				}

				if (t.TopicStarterName.Length > 0)
				{
					LastPosterName = t.LastPosterName.Substring(0, length);
				}


				if (t.LastPosterID != "")
				{
					HyperLink hlLastPoster = new HyperLink();
					hlLastPoster.NavigateUrl = "./Profile.aspx?user=" + t.LastPosterName;

					hlLastPoster.Text = LastPosterName;
					tc.Controls.Add(hlLastPoster);

					tr.Cells.Add(tc);
				}
				else
				{
					Label hlLastPoster = new Label();

					hlLastPoster.Text = LastPosterName;
					tc.Controls.Add(hlLastPoster);

					tr.Cells.Add(tc);
				}









				//Posts
				tc = NewTableCell();
				tc.BackColor = Color1;
				lblPostcount = new Label();
				lblPostcount.Text = t.Posts.ToString();
				tc.Controls.Add(lblPostcount);
				tr.Cells.Add(tc);

				//Views
				tc = NewTableCell();
				tc.BackColor = Color1;
				lblViewcount = new Label();
				lblViewcount.Text = t.Views.ToString();
				tc.Controls.Add(lblViewcount);
				tr.Cells.Add(tc);

				tblTopics.Rows.Add(tr);
			}


			decimal end = System.DateTime.Now.Ticks;
			lblPageloadDuration.Text = "page loaded in " + ((end - start) / 10000000).ToString("n3") + " seconds";
		}

		private TableRow NewTableRow(int backcolorType)
		{
			TableRow row = new TableRow();
			row.BorderStyle = BorderStyle.None;
			row.Width = Unit.Pixel(700);
			row.HorizontalAlign = HorizontalAlign.Justify;


			if (backcolorType == 0)
			{
				//Header
				row.BackColor = Color0;
				row.BorderColor = Color0;

			}
			if (backcolorType == 1)
			{
				//Datarow1
				row.BackColor = Color1;
				row.BorderColor = Color1;

			}
			else if (backcolorType == 2)
			{
				//Datarow2
				row.BackColor = Color2;
				row.BorderColor = Color2;
			}

			return row;
		}

		private TableCell NewTableCell()
		{
			TableCell cell = new TableCell();
			cell.BorderStyle = BorderStyle.None;
			return cell;
		}

	}


}